package cn.trda.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.trda.mapper.AsAccountMapper;
import cn.trda.mapper.AsAccountdetailMapper;
import cn.trda.mapper.AsKeywordsMapper;
import cn.trda.mapper.AsLogsMapper;
import cn.trda.mapper.AsSystemconfigMapper;
import cn.trda.pojo.Account;
import cn.trda.pojo.Accountdetail;
import cn.trda.pojo.Keywords;
import cn.trda.pojo.Logs;
import cn.trda.pojo.User;
import cn.trda.service.KeywordsService;
import cn.trda.util.MySqlPageUtil;
@Service
public class KeywordsServiceImpl implements KeywordsService {

	@Autowired
	private AsKeywordsMapper asKeywordsMapper;
	@Autowired
	private AsAccountMapper accountMapper;
	@Autowired
	private AsAccountdetailMapper accountdetailMapper;
	@Autowired
	private AsSystemconfigMapper systemconfigMapper;
	@Autowired
	private AsLogsMapper logsMapper;
	
	// 预注册冻结资金 类型ID号为9999
	public static final Long YuZhuCeDongJieMoneyKey = 9999L;
	//返回预注册冻结资金 类型ID号为9998
	public static final Long FanHuiYuZhuCeDongJieMoneyKey = 9998L;
	//扣除申请关键词的所有资金 类型ID号为9997
	public static final Long KouChuShenQingGuanJianZiMoneyKey = 9997L;
	// 扣除续费资金 类型ID号为9996
	public static final Long KouChuXuFeiMoneyKey = 9996L;

	@Override
	public Keywords selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return asKeywordsMapper.selectByPrimaryKey(id);
	}

	@Override
	public int getCount(Keywords keywords) {
		// TODO Auto-generated method stub
		return asKeywordsMapper.getCount(keywords);
	}

	@Override
	public List<Keywords> getKeywordsBySearch(Keywords keywords,
			MySqlPageUtil mySqlPageUtil) {
		// TODO Auto-generated method stub
		return asKeywordsMapper.getKeywordsBySearch(keywords,mySqlPageUtil);
	}

	@Override
	public int updateKeywords(Keywords keywords) {
		// TODO Auto-generated method stub
		return asKeywordsMapper.updateKeywords(keywords);
	}

	@Override
	public void tx_changeStatusToOK(Keywords keywords, User sessionUser,
			Date date) {
		// TODO Auto-generated method stub
		int status = keywords.getCheckstatus();
		keywords = asKeywordsMapper.selectByPrimaryKey(keywords.getId());
		keywords.setCheckstatus(status);

		// 返还冻结资金
		Account account = new Account();
		account.setUserid(keywords.getAgentid());
		account = accountMapper.getAccountByUserId(keywords.getAgentid());
		account.setMoney(account.getMoney() + keywords.getPreregfrozenmoney());
		account.setMoneybak(account.getMoney());
		accountMapper.updateByPrimaryKeySelective(account);
		
		//记录账户操作
		Accountdetail accountDetail = new Accountdetail();
		accountDetail.setUserid(sessionUser.getId());
		// 返回预注册冻结资金 类型ID号为9998
		accountDetail.setDetailtype(9998L);
		accountDetail.setDetailtypename("返还预注册冻结资金");
		accountDetail.setMoney(keywords.getPreregfrozenmoney());
		accountDetail.setAccountmoney(account.getMoney());
		accountDetail.setMemo(sessionUser.getUsername() + "对" + keywords.getKeywords()
				+ "进行关键词申请审核通过操作，返还冻结资金："
				+ keywords.getPreregfrozenmoney().toString());
		accountDetail.setDetaildatetime(date);
		accountdetailMapper.insertSelective(accountDetail);

		// 正式通过审核后进行扣款操作
		account.setMoney(account.getMoney() - keywords.getPrice());
		account.setMoneybak(account.getMoney());
		accountMapper.updateByPrimaryKeySelective(account);
		
		//记录账户操作
		accountDetail = new Accountdetail();
		accountDetail.setUserid(sessionUser.getId());
		// 扣除申请关键词的所有资金 类型ID号为9997
		accountDetail.setDetailtype(9997L);
		accountDetail.setDetailtypename("扣除【" + keywords.getKeywords()
				+ "】的正式注册资金。");
		accountDetail.setMoney(keywords.getPrice());
		accountDetail.setAccountmoney(account.getMoney());
		accountDetail.setMemo(sessionUser.getUsername() + "对" + keywords.getKeywords()
				+ "进行关键词申请审核通过自动正式扣款操作，扣除正式注册资金："
				+ keywords.getPrice().toString());
		accountDetail.setDetaildatetime(date);
		accountdetailMapper.insertSelective(accountDetail);

		//修改关键字的属性，清除预注册资金,修改预注册时间为正式的注册时间
		keywords.setPreregfrozenmoney(0.0);
		keywords.setPreregpassdatetime(keywords.getRegpassdatetime());
		asKeywordsMapper.updateKeywords(keywords);

		//记录操作日志
		Logs logs = new Logs();
		logs.setUserid(sessionUser.getId());
		logs.setUsername(sessionUser.getUsername());
		logs.setOperateinfo(sessionUser.getUsername() + "对" + keywords.getKeywords()
				+ "进行关键词状态修改，状态修改为审核通过,并扣款。");
		logs.setOperatedatetime(date);
		logsMapper.insertSelective(logs);
	}

	@Override
	public void tx_changeStatusToNo(Keywords keywords, User sessionUser,
			Date date) {
		// TODO Auto-generated method stub
		int status = keywords.getCheckstatus();
		keywords = asKeywordsMapper.selectByPrimaryKey(keywords.getId());
		keywords.setCheckstatus(status);
		
		// 返还冻结资金
		Account account = new Account();
		account.setUserid(keywords.getAgentid());
		account = accountMapper.getAccountByUserId(keywords.getAgentid());
		account.setMoney(account.getMoney() + keywords.getPreregfrozenmoney());
		account.setMoneybak(account.getMoney());
		accountMapper.updateByPrimaryKeySelective(account);
		
		//添加一条流水记录
		Accountdetail accountDetail = new Accountdetail();
		accountDetail.setUserid(sessionUser.getId());
		// 返回预注册冻结资金 类型ID号为9998
		accountDetail
				.setDetailtype(9998L);
		accountDetail.setDetailtypename("返还预注册冻结资金");
		accountDetail.setMoney(keywords.getPreregfrozenmoney());
		accountDetail.setAccountmoney(account.getMoney());
		accountDetail.setMemo(sessionUser.getUsername() + "对" + keywords.getKeywords()
				+ "进行关键词申请不通过操作，返还冻结资金："
				+ keywords.getPreregfrozenmoney().toString());
		accountDetail.setDetaildatetime(date);
		accountdetailMapper.insertSelective(accountDetail);

		//清除预注册资金
		keywords.setPreregfrozenmoney(0.0);
		//重新设置预注册过期时间为空值
		keywords.setPreregpassdatetime(null);
		asKeywordsMapper.updateKeywords(keywords);

		//新增日志
		Logs logs = new Logs();
		logs.setUserid(sessionUser.getId());
		logs.setUsername(sessionUser.getUsername());
		logs.setOperateinfo(sessionUser.getUsername() + "对" + keywords.getKeywords()
				+ "进行关键词状态修改，状态修改为未审核通过,并返还冻结资金"
				+ keywords.getPreregfrozenmoney() + "。");
		logs.setOperatedatetime(date);
		logsMapper.insertSelective(logs);
	}
	@Override
	public void tx_keywordsXuFei(Keywords keywords, String productType,
			int xufeiYears, Double price, Date date) throws Exception {
		//修改关键字完成续费
		Integer type = Integer.valueOf(productType);
		Integer years = Integer.valueOf(xufeiYears);
		
		keywords = asKeywordsMapper.selectByPrimaryKey(keywords.getId());
		
		keywords.setProducttype(type);
		keywords.setPrice(keywords.getPrice() + price);
		keywords.setServiceyears(keywords.getServiceyears() + years);
		Date xufei = keywords.getRegpassdatetime();
		xufei.setYear(xufei.getYear() + years);
		keywords.setRegpassdatetime(xufei);
		asKeywordsMapper.updateKeywords(keywords);
		
		// 修改账户余额
		Account account = new Account();
		account.setUserid(keywords.getAgentid());
		account = accountMapper.getAccountByUserId(keywords.getAgentid());
		account.setMoney(account.getMoney() - price);
		account.setMoneybak(account.getMoney());
		accountMapper.updateByPrimaryKeySelective(account);
		
		//新增账户明细
		Accountdetail accountDetail = new Accountdetail();
		accountDetail.setUserid(keywords.getAgentid());
		// 扣除续费资金 类型ID号为9996
		accountDetail.setDetailtype(9996L);
		accountDetail.setDetailtypename("扣除续费资金" + years + "年" + price + "元");
		accountDetail.setMoney(price);
		accountDetail.setAccountmoney(account.getMoney());
		accountDetail.setMemo(keywords.getAgentname() + "对"
				+ keywords.getKeywords() + "进行关键词续费操作，扣除续费资金：" + +years + "年"
				+ price + "元");
		accountDetail.setDetaildatetime(date);
		accountdetailMapper.insertSelective(accountDetail);

		//新增日志
		Logs logs = new Logs();
		logs.setUserid(keywords.getAgentid());
		logs.setUsername(keywords.getAgentname());
		logs.setOperateinfo(keywords.getAgentname() + "对"
				+ keywords.getKeywords() + "进行关键词续费操作，扣除续费资金：" + +years + "年"
				+ price + "元");
		logs.setOperatedatetime(date);
		logsMapper.insertSelective(logs);
	}

	@Override
	public Keywords checkKeywords(String keywords) {
		return asKeywordsMapper.selectByKeywords(keywords);
		 
	}

	@Override
	public void saveKeywords(Keywords keywords, User agent, Date date) {
		// TODO Auto-generated method stub
		//保存关键字
		asKeywordsMapper.insert(keywords);
		//修改账户余额
		Account account = new Account();
		account.setUserid(agent.getId());
		account = accountMapper.getAccountByUserId(agent.getId());
		account.setMoney(account.getMoney() - (keywords.getPreregfrozenmoney()));
		account.setMoneybak(account.getMoney());
		accountMapper.updateByPrimaryKey(account);
		//新增账户明细
		Accountdetail accountDetail = new Accountdetail();
		accountDetail.setUserid(agent.getId());
		// 预注册冻结资金 类型ID号为9999
		accountDetail.setDetailtype(YuZhuCeDongJieMoneyKey);
		accountDetail.setDetailtypename("预注册冻结资金");
		accountDetail.setMoney(-keywords.getPreregfrozenmoney());
		accountDetail.setAccountmoney(account.getMoney());
		accountDetail.setMemo(agent.getUsername() + "对" + keywords.getKeywords()
			+ "进行关键词申请预注册操作，冻结资金："
			+ keywords.getPreregfrozenmoney().toString());
		accountDetail.setDetaildatetime(date);
		accountdetailMapper.insert(accountDetail);
	}

}
